package people.paper.tv.activity.home.modual.rmrb.model.impl;

import android.content.Context;
import android.util.Log;

import com.coocaa.app.support.net.Callback;
import com.coocaa.app.support.net.Result;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import people.paper.tv.R;
import people.paper.tv.activity.home.model.MainDataObject;
import people.paper.tv.activity.home.model.MainServerHttpService;
import people.paper.tv.activity.home.modual.rmrb.model.RMRBModel;
import retrofit2.Call;

public class RMRBModelImpl implements RMRBModel {
    private static final int DAYS = 5;
    private static final long DAY_LENGTH = 1000 * 3600 * 24;
    private List<SupportDay> supportDays = new ArrayList<>();
    private Context context;

    @Override
    public void create(Context context) {
        this.context = context;
        long current = System.currentTimeMillis();//当前时间毫秒数
        long zero = current / DAY_LENGTH * DAY_LENGTH - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
        for (int i = 0; i < DAYS; i++) {
            SupportDay day = new SupportDay();
            day.timestamp = zero - i * DAY_LENGTH;
            day.name = formatDate(day.timestamp);
            supportDays.add(day);
        }
    }

    @Override
    public String formatDate(long time) {
        String format = context.getString(R.string.rmrb_time_format);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(time)));
    }

    @Override
    public void getPaper(int day, final Callback<Result<List<MainDataObject.RMRBObject>>> callback) {
        day = Math.abs(day);
        if (day > supportDays.size() - 1)
            return;
        SupportDay _day = supportDays.get(day);

        String date = String.valueOf(_day.timestamp);
        List<String> params = new ArrayList<>();
        params.add(date);
        MainServerHttpService.Signature signature = MainServerHttpService.httpService.getSignatur(params);
        Call<MainDataObject.RMRBData> call = MainServerHttpService.httpService.postRMRB(date, signature.timestamp, signature.nonce, signature.signature);
        MainServerHttpService.httpService.execute(call, new Callback<Result<MainDataObject.RMRBData>>() {
            @Override
            public Ret onResult(Result<MainDataObject.RMRBData> result) {
                if (result.exception != null && result.data != null)
                    return callback.onResult(Result.cc(result, result.data.obj));
                if (result.data != null)
                    return callback.onResult(Result.cc(result, result.data.obj));
                return new Ret(0, "");
            }
        });
    }

    @Override
    public void getNewsContent(String date, String layoutId, String newsId,
                               final Callback<Result<String>> callback) {
        List<String> params = new ArrayList<>();
        params.add(date);
        params.add(layoutId);
        params.add(newsId);
        MainServerHttpService.Signature signature = MainServerHttpService.httpService.getSignatur(params);
        Call<MainDataObject.RMRBContentData> call = MainServerHttpService.httpService.postRMRBContent(date, layoutId, newsId, signature.timestamp, signature.nonce, signature.signature);
        MainServerHttpService.httpService.execute(call, new Callback<Result<MainDataObject.RMRBContentData>>() {
            @Override
            public Ret onResult(Result<MainDataObject.RMRBContentData> result) {
                Log.d("RMRB", "duration:" + result.duration);
                if (result.data != null) {
                    return callback.onResult(Result.cc(result, result.data.obj.get(0).content));
                }
                if (result.exception != null) {
                    Log.d("RMRB", "exception:" + result.exception.toString());
                }
                return new Ret(0, "");
            }
        });
    }

    @Override
    public List<SupportDay> getSupportDays() {
        return supportDays;
    }
}
